package com.lzs.adx.web.modules.adx.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.lzs.adx.commmon.page.PageData;
import com.lzs.adx.commmon.service.impl.CrudServiceImpl;
import com.lzs.adx.web.modules.adx.dao.AccountDao;
import com.lzs.adx.web.modules.adx.dto.AccountDTO;
import com.lzs.adx.web.modules.adx.entity.AccountEntity;
import com.lzs.adx.web.modules.adx.service.AccountService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**
 * 投放的账户管理
 *
 * @author wangzhijun 825400558@qq.com
 * @since 1.0.0 2023-11-29
 */
@Service
public class AccountServiceImpl extends CrudServiceImpl<AccountDao, AccountEntity, AccountDTO> implements AccountService {

    @Resource
    private AccountDao accountDao;


    @Override
    public QueryWrapper<AccountEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String name = (String)params.get("name");
        String userName = (String)params.get("userName");
        String business = (String)params.get("business");
        String productChannel = (String)params.get("productChannel");
        String mediaChannel = (String)params.get("mediaChannel");
        String status = (String)params.get("status");
        Boolean fakePerNotNull = (Boolean) params.get("fakePerNotNull");
        Integer muiltReportInd = (Integer) params.get("muiltReportInd");

        QueryWrapper<AccountEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        wrapper.like(StringUtils.isNotBlank(name), "name", name);
        wrapper.eq(StringUtils.isNotBlank(userName), "user_name", userName);
        wrapper.eq(StringUtils.isNotBlank(business), "business", business);
        wrapper.eq(StringUtils.isNotBlank(productChannel), "product_channel", productChannel);
        wrapper.eq(StringUtils.isNotBlank(mediaChannel), "media_channel", mediaChannel);
        wrapper.eq(StringUtils.isNotBlank(status), "status", status);
        wrapper.eq(Objects.nonNull(muiltReportInd), "muilt_report_ind", muiltReportInd);

        wrapper.like(Objects.nonNull(fakePerNotNull), "fake_per", fakePerNotNull);

        wrapper.orderByDesc("update_time");

        return wrapper;
    }

    @Override
    public PageData<AccountDTO> page(Map<String, Object> params) {
        PageData<AccountDTO> page = super.page(params);
        if(Objects.nonNull(page)){
            if(CollectionUtil.isNotEmpty(page.getList())){
                page.getList().forEach(c->{
                    if(StringUtils.isNotBlank(c.getSspExtend())){
                        JSONObject jsonObject = JSONObject.parseObject(c.getSspExtend());
                        if(jsonObject.containsKey("per")){
                            c.setPer(jsonObject.getDouble("per"));
                        }
                    }
                });
            }
        }
        return page;
    }

    @Override
    public AccountDTO getDetail(String id) {
        AccountDTO accountDTO=new AccountDTO();
        AccountEntity accountEntity = accountDao.selectById(id);
        BeanUtils.copyProperties(accountEntity,accountDTO);

        return accountDTO;
    }

    @Override
    public void deleteByIds(String[] ids) {

        accountDao.deleteBatchIds(Lists.newArrayList(ids));
    }


}